package nku.csc364.updatedatabase;

import nku.csc364.R;
import nku.csc364.databasehelper.DataHelper;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;

public class UpdateDatabaseProductActivity extends Activity{
	Spinner productCategory, productProduct; 
	EditText productName, productDesc; 
	ImageView productImage;
	Integer imageInteger;
	DataHelper dh = null; 
	private Integer[] Imgid = {
			R.drawable.proj3_1, 
			R.drawable.proj3_2, 
			R.drawable.proj3_3, 
			R.drawable.proj3_4, 
			R.drawable.proj3_5, 
			R.drawable.proj3_6, 
			R.drawable.proj3_7,
			R.drawable.proj3_8,
			R.drawable.proj3_9,
			R.drawable.proj3_10,
			R.drawable.proj3_11,
			R.drawable.proj3_12,
			R.drawable.proj3_13,
			R.drawable.proj3_14 
			};
	
	public  void openCategory(View view){
		Intent launchCategory = new Intent(this, UpdateDatabaseCategoryActivity.class);
		startActivityForResult(launchCategory, 1);
	}
	
	public void openItem(View view){
		Intent launchItem = new Intent(this, UpdateDatabaseItemActivity.class);
		startActivityForResult(launchItem, 1);
	}
	
	public void openProduct(View view){
		Intent launchProduct = new Intent(this, UpdateDatabaseProductActivity.class);
		startActivityForResult(launchProduct, 1);
	}
	
	public void openQOH(View view){
		Intent launchQOH = new Intent(this, UpdateDatabaseQOHActivity.class);
		startActivityForResult(launchQOH, 1);
	}
	
	public void openSupplier(View view){
		Intent launchSupplier = new Intent(this, UpdateDatabaseSupplierActivity.class);
		startActivityForResult(launchSupplier, 1);
	}
	
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.product);
        
        productCategory = (Spinner)findViewById(R.id.productSpinner1);
        productProduct = (Spinner)findViewById(R.id.productSpinner2);
        productName = (EditText)findViewById(R.id.productEditText1);
        productDesc = (EditText)findViewById(R.id.productEditText2);
        productImage = (ImageView)findViewById(R.id.productImageView1);
        
        dh = DataHelper.getDataHelper(this);
        setCategorySpinner();
        productCategory.setPrompt("Select...");
        setProductSpinner();
        productProduct.setPrompt("Select...");
        productProduct.setOnItemSelectedListener(new MyOnProductSelectedListener());
    }
    
	  public class MyOnProductSelectedListener implements OnItemSelectedListener {
		    public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
		    	if(!parent.getItemAtPosition(pos).toString().equals(" ")){
		    		Cursor mCursor = dh.getItem(parent.getItemAtPosition(pos).toString());
			    	int i = 0;
					for(String x : mCursor.getColumnNames()){
						if(i == 0){
							String category = mCursor.getString(mCursor.getColumnIndexOrThrow(x));
							ArrayAdapter<CharSequence> categoryArray = getCategoryAdapter();
							int index = 0;
							for(int j = 0; j < categoryArray.getCount(); j++){
								if(category.equals(categoryArray.getItem(j))){
									index = j;
								}
							}
							productCategory.setSelection(index);
						}
						else if( i == 1){
							productName.setText(mCursor.getString(mCursor.getColumnIndexOrThrow(x)));	
						}
						else if ( i == 2){
							productDesc.setText(mCursor.getString(mCursor.getColumnIndexOrThrow(x)));
						}
						else if ( i == 3){
							setPicture(mCursor.getInt(mCursor.getColumnIndexOrThrow(x)));
						}
						i++;
					}
			    }
		    	else{
		    		clean();
		    	}
		    }
		    public void onNothingSelected(@SuppressWarnings("rawtypes") AdapterView parent) {
		      // Do nothing.
		    }
	}
	
	 private void clean(){
		 productName.setText("");
		 productDesc.setText(""); 
		 this.productCategory.setSelection(0);
		 this.productProduct.setSelection(0);
		 setPicture(0);
	}
    
    
    private void setProductSpinner() {
    	 productCategory.setAdapter(getCategoryAdapter());
	}
    
	  @SuppressWarnings("unchecked")
		public ArrayAdapter<CharSequence> getCategoryAdapter(){
				    @SuppressWarnings("rawtypes")
					ArrayAdapter itemAdapter = new ArrayAdapter(this,android.R.layout.simple_spinner_item, dh.selectAllCategorys());	 
			        itemAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
			        itemAdapter.insert(" ", 0);
			        return itemAdapter;
		}

	private void setCategorySpinner() {
		productProduct.setAdapter(getProductAdapter());
	}
	
	  @SuppressWarnings("unchecked")
		public ArrayAdapter<CharSequence> getProductAdapter(){
				    @SuppressWarnings("rawtypes")
					ArrayAdapter itemAdapter = new ArrayAdapter(this,android.R.layout.simple_spinner_item, dh.selectAllProducts());	 
			        itemAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
			        itemAdapter.insert(" ", 0);
			        return itemAdapter;
		}

	public void doAddProduct(View view){
    	dh.insertProduct(this.productCategory.getSelectedItem().toString(),
    			this.productName.getText().toString(), 
    			this.productDesc.getText().toString(), 
    			this.imageInteger);
    }
    
    public void doDeleteProduct(View view){
    	dh.deleteProduct(this.productProduct.getSelectedItem().toString());
    }
    
    public void doUpdateProduct(View view){
    	doAddProduct(view);
    	doDeleteProduct(view);
    }
    
	public void doSelectPicture(View view){
		Intent launchSelectPicture = new Intent(this, SelectPictureActivity.class);
		startActivityForResult(launchSelectPicture, 0);
	}
	
	protected void onActivityResult(int requestCode,int resultCode, Intent data) {
		if (resultCode == RESULT_OK) 
		{
			int pictureID = data.getExtras().getInt("pictureNumber");
			setPicture(pictureID);	
		}
		else{
			Toast
			  .makeText(this, "Picture not passed back!",Toast.LENGTH_LONG)
			  .show();
		}
		super.onActivityResult(requestCode, resultCode, data);
	}
	
	private void setPicture(int pictureID) {
		if(pictureID > -1 && pictureID < 14){
			productImage.setImageResource(Imgid[pictureID]);
			imageInteger = pictureID;
		}
		else{
			Toast
			  .makeText(this, "Picture reference is incorrect!",Toast.LENGTH_LONG)
			  .show();
		}
	}
}
